<template>
	<view class="container">
		<!-- 顶部状态栏 -->
		<view class="tab-header">
			<view 
				class="tab-item" 
				v-for="(item, index) in tabs" 
				:key="index"
				:class="{active: currentTab === index}"
				@click="switchTab(index)"
			>
				<text>{{item}}</text>
				<view class="tab-line"></view>
			</view>
		</view>

		<!-- 优惠券列表 -->
		<view class="coupon-list">
			<template v-if="couponList.length > 0">
				<view class="coupon-item" v-for="(item, index) in couponList" :key="item.id" @click="showDetail(item.id)">
					<view class="left flex">
						<view class="amount">
							<text class="value">{{ (item.discountPrice / 100).toFixed(0) }}</text>
						</view>
						<text class="desc">满{{ (item.usePrice / 100).toFixed(0) }}可用</text>
					</view>
					<view class="right">
						<text class="title">{{item.name}}</text>
						<text class="date">有效期: {{formatDate(item.validStartTime)}} - {{formatDate(item.validEndTime)}}</text>
						<view class="flex" style="justify-content: space-between;">
							<view class="flex" style="align-items: center;">
								<text class="rules" @click.stop="showRules(item)">使用规则</text>
								<u-icon name="arrow-right" color="#868686" size="22rpx"></u-icon>
							</view>
							<!-- <view class="btn" v-if="item.status === 1" @click.stop="useCoupon(item)">
								<text>去使用</text>
							</view> -->
						</view>
					</view>
					<view class="status flex" :class="{'no-user': item.status === 1}">
						<text class="text">{{ item.status == 1 ? '待使用' :  item.status == 2 ?'已使用': '已失效'  }}</text>
					</view>
				</view>
			</template>
			
			<view v-else class="empty-state">
				<u-empty mode="coupon" text="暂无优惠券"></u-empty>
			</view>
			
			<!-- 加载更多 -->
			<u-loadmore v-if="couponList && couponList.length > 0" :status="loadMoreStatus" />
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				currentTab: 0,
				tabs: ['全部', '待使用', '已使用', '已失效'],
				couponList: [],
				pageNo: 1,
				pageSize: 15,
				total: 0,
				loading: false,
				finished: false
			}
		},
		computed: {
			loadMoreStatus() {
				if (this.loading) return 'loading'
				if (this.finished) return 'nomore'
				return 'loadmore'
			}
		},
		onLoad() {
			this.getCouponList()
		},
		methods: {
			formatDate(timestamp) { 
				var da = new Date(timestamp);
				var year = da.getFullYear();
				var month = da.getMonth()+1;
				var date = da.getDate();
			    return [year,month,date].join('/')
			},
			getCouponList(reset = false) {
				if (this.loading || (this.finished && !reset)) return
				
				if (reset) {
					this.pageNo = 1
					this.finished = false
					this.couponList = []
				}
				
				this.loading = true
				const data = {
					pageNo: this.pageNo,
					pageSize: this.pageSize
				};
				if (this.currentTab != 0) {
					data.status = this.getStatus()
				}
				this.$http_api.couponList(data).then(res => {
					const { list, total } = res
					if (reset) {
						this.couponList = list
					} else {
						this.couponList = [...this.couponList, ...list]
					}
					this.total = total
					this.finished = this.couponList.length >= total
					this.pageNo++
				}).finally(() => {
					this.loading = false
				})
			},
			
			switchTab(index) {
				this.currentTab = index
				this.getCouponList(true)
			},
			
			getStatus() {
				return this.currentTab;
			},
			
			// Add onReachBottom lifecycle
			onReachBottom() {
				this.getCouponList()
			},
			
			useCoupon(coupon) {
				if(coupon.status !== 1) return
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
			showRules(coupon) {
				uni.showModal({
					title: '使用规则',
					content: coupon.description || '暂无规则说明',
					showCancel: false
				})
			},
			showDetail(id) {
				uni.navigateTo({
					url: '/pages/user/users/coupon/detail?id=' + id
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.container {
		background: #F6FAFF;
		box-sizing: border-box;
	}
	
	.tab-header {
		display: flex;
		background: #fff;
		padding: 20rpx 0 0 0;
		margin-bottom: 20rpx;
		position: sticky;
		top: var(--window-top);
		z-index: 99;
		
		.tab-item {
			flex: 1;
			display: flex;
			flex-direction: column;
			align-items: center;
			position: relative;
			
			text {
				font-size: 32rpx;
				color: #333;
				font-family: "PINGFANG";
				font-weight: 400;
			}
			
			.tab-line {
				width: 40rpx;
				height: 8rpx;
				background: transparent;
				border-radius: 69rpx;
				margin-top: 10rpx;
			}
			
			&.active {
				text {
					color: #42E0A2;
					font-weight: 600;
				}
				
				.tab-line {
					background: #42E0A2;
				}
			}
		}
	}
	
	.coupon-list {
		padding: 0 20rpx 80rpx 20rpx;
		
		.coupon-item {
			display: flex;
			background: #fff;
			border-radius: 24rpx;
			margin-bottom: 20rpx;
			overflow: hidden;
			position: relative;
			
			.status {
				position: absolute;
				right: 0;
				top: 0;
				background: #AAAAAA;
				width: 90rpx;
				height: 40rpx;
				justify-content: center;
				border-bottom-left-radius: 28rpx;
				.text {
					font-size: 20rpx;
					font-weight: 400;
					color: #fff;
					font-family: "PINGFANG";
				}
				&.no-user {
					background: #FF4600;
				}
			}
			
			.left {
				flex-direction: column;
				justify-content: center;
				width: 200rpx;
				height: 200rpx;
				background: linear-gradient(140deg, #1C6EFB, #02D9C3);
				position: relative;
				
				&::after, &::before {
					content: '';
					width: 20rpx;
					height: 20rpx;
					border-radius: 100%;
					background: #fff;
					position: absolute;
					right: -10rpx;
				}
				
				&::before {
					top: -10rpx;
				}
				
				&::after {
					bottom: -10rpx;
				}
				
				.amount {
					display: flex;
					align-items: baseline;
					
					.value {
						font-size: 80rpx;
						color: #fff;
						font-weight: 600;
						font-family: "PINGFANG";
					}
				}
				
				.desc {
					font-size: 24rpx;
					color: #fff;
					font-weight: 600;
				}
			}
			
			.right {
				flex: 1;
				padding-left: 30rpx;
				display: flex;
				flex-direction: column;
				justify-content: center;
				
				.title {
					font-size: 32rpx;
					color: #333;
					font-weight: 600;
					font-family: "PINGFANG";
				}
				
				.date {
					font-size: 24rpx;
					color: #999;
					margin: 10rpx 0 20rpx;
				}
				
				.btn {
					width: 160rpx;
					height: 60rpx;
					background: #42E0A2;
					border-radius: 30rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					
					text {
						font-size: 24rpx;
						font-family: "PINGFANG";
						color: #fff;
					}
					
					&.used {
						background: #999;
					}
					
					&.expired {
						background: #ddd;
					}
				}
				
				.rules {
					font-size: 24rpx;
					color: #999;
				}
			}
		}
	}
	
	.empty-state {
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 100rpx 0;
	}
</style>